#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "ndmp.h"

void printusage()
{
	printf("\nndmphbactl\tadd\t<ndmpserver> <ndmpuser> <ndmpuserpasswd> <ndmptape>\n");			
	printf("ndmphbactl\tremove\t<ndmpserver> <ndmpuser> <ndmpuserpasswd> <ndmptape> <sessionid>\n");			
	printf("ndmphbactl\tlsdev\t<ndmpserver> <ndmpuser> <ndmpuserpasswd>\n");			
	printf("ndmphbactl\tlist\n");
	printf("\nActions:\n");
	printf("--------\n");
	printf("lsdev\t\t-List device on the NDMP server\n");
	printf("add\t\t-Add a NDMP session and start the SCSI host\n");
	printf("remove\t\t-Remove a NDMP session\n");
	printf("list\t\t-List the NDMP sessions added\n");
	printf("\nSteps:\n");
	printf("------\n");
	printf("1. Run 'make' to make the kernel modules and user agent\n");
	printf("2. Insert the module to the kernel, 'insmod ndmphba.ko'\n");
	printf("3. Find the major number for the control device, 'cat /proc/devices|grep ndmphba'\n");
	printf("4. Create the control device, 'mknod ndmphba c <major> 0'\n");
	printf("5. Use the ndmphbactl commands to manage the NDMP tape drives or robots\n\n");
}
/* ndmp_start_hba - Do the ioctl to create a NDMP session in kernel */
int ndmp_start_hba(struct ndmp_session_detail *s, char *devname)
{
	int fd;
        if ( (fd=open(devname,O_RDWR)) < 0 ) { printf("Unable to open device: %s\nMake sure that the module is loaded and ndmphba device is created using 'mknod ndmphba c <major> 0' in current directory\n", devname); return 1; }
        if ( ioctl(fd,NDMP_SCSI_CREATE_SESSION_IOCTL,s) ) return 1;
	close(fd);
	return 0;
}

/* ndmp_stop_hba - Do the ioctl to remove a NDMP session in kernel */
int ndmp_stop_hba(struct ndmp_session_detail *s, char *devname)
{
	int fd;
        if ( (fd=open(devname,O_RDWR)) < 0 ) { printf("Unable to open device: %s\nMake sure that the module is loaded and ndmphba device is created using 'mknod ndmphba c <major> 0' in current directory\n", devname); return 1; }
        if ( ioctl(fd,NDMP_SCSI_TERMINATE_SESSION_IOCTL,s) ) return 1;
	close(fd);
	return 0;
}

/* ndmp_stop_hba - List all the NDMP sessions and attributes in the kernel */
int ndmp_list_hba(struct ndmp_session_detail *s, char *devname, char *buffer)
{
	int fd;
        if ( (fd=open(devname,O_RDWR)) < 0 ) { printf("Unable to open device: %s\nMake sure that the module is loaded and ndmphba device is created using 'mknod ndmphba c <major> 0' in current directory\n", devname); return 1; }
        if ( ioctl(fd,NDMP_SCSI_LIST_SESSION_IOCTL,buffer) ) return 1;
	close(fd);
	return 0;
}

main(int argc, char *argv[])
{
	struct ndmp_session_detail s;
	char buf[2048];
	int x;


	memset(&s,0,sizeof(struct ndmp_session_detail));
	s.status=-1;

	if ( argc < 2 ) {
		printusage();
		exit(0);
	}

	if ( strcmp(argv[1],"remove" ) == 0 ) {

		if ( argc != 7 ) {
			printusage();
			exit(0);
		}

		strcpy(s.ndmp_server,argv[2]);
		strcpy(s.user,argv[3]);
		strcpy(s.passwd,argv[4]);
		strcpy(s.device,argv[5]);

		s.id=atoi(argv[6]);

		
		if ( ndmp_stop_hba(&s, "ndmphba") ) {
			printf("Error removing NDMP session :%d\nUse list to find out the running sessions\n", s.id);
			exit(1);
		}
		printf("The IOCTL executed successfully, check dmesg/messages file to see if the session remove was successful or not\n");
	} else if ( strcmp(argv[1],"add") == 0 ) {

		if ( argc != 6 ) {
			printusage();
			exit(0);
		}

		strcpy(s.ndmp_server,argv[2]);
		strcpy(s.user,argv[3]);
		strcpy(s.passwd,argv[4]);
		strcpy(s.device,argv[5]);

		printf("Probing device(%s): ");
		x=open_tapedrive(&s, s.device);
		printf("%s\n",errorp(x));
		if ( x ) exit(1);

		if ( ndmp_start_hba(&s, "ndmphba") ) {
			printf("Error creating NDMP session\n");
			exit(1);
		}
		printf("The IOCTL executed successfully, check dmesg/messages to see the results\n");
	} else if ( strcmp(argv[1],"list") == 0 ) {
		if ( argc != 2 ) {
			printusage();
			exit(0);
		}
		if ( ndmp_list_hba(&s, "ndmphba", buf) ) {
			printf("Error listing NDMP session\n");
			exit(1);
		}
		printf("%s\n",buf);
	} else if ( strcmp(argv[1],"lsdev") == 0 ) {

		if ( argc != 5 ) {
			printusage();
			exit(0);
		}

		strcpy(s.ndmp_server,argv[2]);
		strcpy(s.user,argv[3]);
		strcpy(s.passwd,argv[4]);
	
		x=list_devices(&s);
		printf("\nList devices: %s\n", errorp(x));
	} else printusage();

}
